package com.cgw.game.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;

import com.cgw.game.R;
import com.cgw.game.adapter.PrescriptionAnswerAdapter;
import com.cgw.game.entity.PrescriptionAnswerDataEntity;
import com.cgw.game.entity.PrescriptionAnswerEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class PrescriptionAnswerActivity extends AppCompatActivity {
    RecyclerView recycler;
    PrescriptionAnswerAdapter answerAdapter;
    List<PrescriptionAnswerEntity> answers;
    List<PrescriptionAnswerDataEntity> data;
    private Handler handler = new Handler();
    int step = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prescription_answer);
        recycler = findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        data = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            List<String> item = new ArrayList<>();
            item.add("问题"+(i+1)+"的答案1");
            item.add("问题"+(i+1)+"的答案2");
            item.add("问题"+(i+1)+"的答案3");
            item.add("问题"+(i+1)+"的答案4");
            data.add(new PrescriptionAnswerDataEntity("问题"+(i+1),item));
        }
        answers = new ArrayList<>();
        answers.add(new PrescriptionAnswerEntity(null,
                "waiting，你好，请根据你的第一感觉，按真实情况开始作答，作答过程中可上翻切换选项修改答案，你现在准备好了吗？",0));
        recycler.setAdapter(answerAdapter = new PrescriptionAnswerAdapter(answers));
        answerAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        answerAdapter.setListener(new PrescriptionAnswerAdapter.Listener() {
            @Override
            public void start() {
                answerAdapter.notifyDataSetChanged();
                answerAdapter.addData(new PrescriptionAnswerEntity(null,
                        "可以开始了",1));
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        answerAdapter.addData(new PrescriptionAnswerEntity(null,
                                data.get(step).getTitle(),0,step));

                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                answerAdapter.addData(new PrescriptionAnswerEntity(data.get(step).getContent(),
                                        "",1,step));
                                step++;
                                answerAdapter.setIsload(false);
                            }
                        },500);
                    }
                },500);

                /*answerAdapter.addData(new PrescriptionAnswerEntity(null,
                        "可以开始了",1));
                answerAdapter.addData(new PrescriptionAnswerEntity(null,
                        data.get(step).getTitle(),0,step));
                answerAdapter.addData(new PrescriptionAnswerEntity(data.get(step).getContent(),
                        "",1,step));*/
//              step++;
            }

            @Override
            public void next(int pos) {
                if(pos==(step-1)&&step < data.size()){
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            answerAdapter.addData(new PrescriptionAnswerEntity(null,
                                    data.get(step).getTitle(),0,step));
                            recycler.scrollToPosition(answerAdapter.getData().size()-1);
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    answerAdapter.addData(new PrescriptionAnswerEntity(data.get(step).getContent(),
                                            "",1,step));
                                    recycler.scrollToPosition(answerAdapter.getData().size()-1);
                                    step++;
                                    answerAdapter.setIsload(false);
                                }
                            },500);
                        }
                    },500);
                }
            }
        });
    }
}
